package com.wdcloud.framework_business.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wdcloud.framework_business.R;

/**
 * Info:
 * Created by Umbrella.
 * CreateTime: 2021/3/4 16:14
 */
public class ProgressDialog {
    static Dialog dialog = null;

    public static void showDialog(Context context) {
        if (dialog != null && dialog.isShowing())
            return;
        closeDialog(); // createView之前先colseDialog一下，关闭之前的diglog（如果存在）
        if (context != null && !((Activity) context).isFinishing()) {
            LayoutInflater inflate = LayoutInflater.from(context);
            View v = inflate.inflate(R.layout.progress_dialog, null);
            dialog = new Dialog(context, R.style.loading_dialog);
            dialog.show();
            dialog.setContentView(v, new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            setlock();
        }
    }

    public static void showDialogForHTML(Context context) {
        if (dialog != null && dialog.isShowing())
            return;
        if (context != null && !((Activity) context).isFinishing()) {
            LayoutInflater inflate = LayoutInflater.from(context);
            @SuppressLint("InflateParams") View v = inflate.inflate(R.layout.progress_dialog, null);
            dialog = new Dialog(context, R.style.loading_dialog);
            dialog.show();
            dialog.setContentView(v, new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            setlock();
        }
    }

    public static void showDialog(Context context, String tips) {
        closeDialog(); // createView之前先colseDialog一下，关闭之前的diglog（如果存在）
        LayoutInflater inflate = LayoutInflater.from(context);
        @SuppressLint("InflateParams")
        View v = inflate.inflate(R.layout.progress_dialog, null);
        LinearLayout layout = v.findViewById(R.id.ll_waiting_dialog);
        TextView tv_tip = v.findViewById(R.id.textView_tips);
        tv_tip.setText(tips);
        tv_tip.setVisibility(View.VISIBLE);
        dialog = new Dialog(context, R.style.loading_dialog);
        dialog.show();
        dialog.setContentView(layout, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setlock();
    }

    public static void setlock() {// 锁定屏幕
        dialog.setCanceledOnTouchOutside(false);
    }

    public static void setunlock() {// 锁定屏幕
        dialog.setCanceledOnTouchOutside(true);
    }

    private static void showDialog() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public static void closeDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            /**防止内存泄漏*/
            dialog = null;
        }
    }

    public static void closeDialog(Context context) {
        if (dialog != null && dialog.isShowing()) {
            /**防止内存泄漏*/
            dialog.dismiss();
            dialog = null;
        }
    }
}